#ifndef LUA_INTERFACE_HPP
#define LUA_INTERFACE_HPP

#include "request.hpp"
#include "reply.hpp"
#include "boost/thread.hpp"
#include <pthread.h>
#include "l_utility.hpp"
#include "dynamic-load/dll_manager.hpp"

namespace http {
    namespace server3 {

class LuaInterface{
private:
    lua_State *L; // we need L to close Lua State latter
    lua_State *cL; /* Coroutine Lua State */
    lua_State *new_cL;
    lua_State *old_cL;
    time_t *last_update_time;
    pthread_mutex_t mutex_update_LVM;
    boost::mutex mutex;
    void update_LVM();
    // idiom to prevent copying: don't implement these
    LuaInterface( LuaInterface const&);
    void operator=( LuaInterface const&);

public:
    static std::string LuaBusinessDir;
    static std::string LuaDir;
    static std::string LuaLibDir;
    LuaInterface();
    void do_business_interface(request& i_request, reply& o_reply);

    /* createNewCoroutine is not thread-safe */
    void createNewCoroutine();
    void lockLVM() { mutex.lock();}
    void unlockLVM() { mutex.unlock();}
    ~LuaInterface();
};

    }
}

#endif
